
var cardMgr = require("./cardMgr");

const CardDecor = cardMgr.CardDecor;


class DXCardMgr extends cardMgr.CardMgr {
    
    // 获取牌的点数
    getCardPoint(card) {
        if (this.isCard(card, 1, CardDecor.SPADE)) return 6;
        return card / 10;
    }

    // 是否是皇帝对
    isKingPair(cards) {
        // 钓蟹只有2张牌
        if (cards.length != 2) return false;
        if (cards[0] == cards[1]) return false;
        if (!this.hasCard(cards, 1, CardDecor.SPADE)) return false;
        if (!this.hasCard(cards, 3, CardDecor.SPADE)) return false;
        return true;
    }
    
    // 是否是对子
    isPair(cards) {
        if (cards.length != 2) return false;
        if (cards[0] == cards[1]) return false;
        if (this.getCardValue(cards[0]) != this.getCardValue(cards[1])) return false;
        if (this.isRedCard(cards[0]) && this.isRedCard(cards[1])) return true;
        if (this.isBlackCard(cards[0]) && this.isBlackCard(cards[1])) return true;
        return false;
    }
    
    // 天对
    isSkyPair(cards) {
        if (cards.length != 2) return false;
        if (!this.isPair(cards)) return false;
        return this.getCardValue(cards[0]) == 12;
    }
    
    // 地对
    isEarthPair(cards) {
        if (cards.length != 2) return false;
        if (!this.isPair(cards)) return false;
        return this.getCardValue(cards[0]) == 2;
    }
    
    // 人对
    isHumanPair(cards) {
        if (cards.length != 2) return false;
        if (!this.isPair(cards)) return false;
        if (!this.isRedCard(cards[0])) return false;
        return this.getCardValue(cards[0]) == 8;
    }
    
    // 鹅对/和对
    isGoosePair(cards) {
        if (cards.length != 2) return false;
        if (!this.isPair(cards)) return false;
        if (!this.isRedCard(cards[0])) return false;
        return this.getCardValue(cards[0]) == 4;
    }
    
    // 板凳对+梅花对+长三对
    isBenchPair(cards) {
        if (cards.length != 2) return false;
        if (!this.isPair(cards)) return false;
        if (!this.isBlackCard(cards[0])) return false;
        var cardValue = this.getCardValue(cards[0]);
        return cardValue == 4 || cardValue == 6 || cardValue == 10;
    }
    
    // 斧头对+红头对+霖六对+脚七对
    isAxePair(cards) {
        if (cards.length != 2) return false;
        if (!this.isPair(cards)) return false;
        if (!this.isRedCard(cards[0])) return false;
        var cardValue = this.getCardValue(cards[0]);
        return cardValue == 6 || cardValue == 7 || cardValue == 10 || cardValue == 11;
    }
    
    // 鸡仔对+红根对+风吹对
    isWindPair(cards) {
        if (cards.length != 2) return false;
        if (!this.isPair(cards)) return false;
        var cardValue = this.getCardValue(cards[0]);
        if (this.isRedCard(cards[0])) {
            return cardValue == 5 || cardValue == 9;
        } else {
            return cardValue == 7 || cardValue == 8;
        }
    }
    
    // 天九王
    isSkyNine(cards) {
        if (cards.length != 2) return false;
        if (this.getCardValue(cards[0]) == this.getCardValue(cards[1])) return false;
        if (!this.hasCard(cards, 12)) return false;
        if (!this.hasCard(cards, 9)) return false;
        return true;
    }
    
    // 天杠
    isSkyBar(cards) {
        if (cards.length != 2) return false;
        if (this.getCardValue(cards[0]) == this.getCardValue(cards[1])) return false;
        if (!this.hasCard(cards, 12)) return false;
        if (!this.hasCard(cards, 8)) return false;
        return true;
    }
    
    // 地杠
    isEarthBar(cards) {
        if (cards.length != 2) return false;
        if (this.getCardValue(cards[0]) == this.getCardValue(cards[1])) return false;
        if (!this.hasCard(cards, 2)) return false;
        if (!this.hasCard(cards, 8)) return false;
        return true;
    }
    
    // 计算杂牌点数
    getMixCardPoint(cards) {
        if (cards.length != 2) return -1;
        var cardValue = this.getCardPoint(cards[0]) + this.getCardPoint(cards[1]);
        return cardValue % 10;
    }

    calculate(cards) {
        if (this.isKingPair(cards)) return 1000;
        if (this.isSkyPair(cards)) return 990;
        if (this.isEarthPair(cards)) return 980;
        if (this.isHumanPair(cards)) return 970;
        if (this.isGoosePair(cards)) return 960;
        if (this.isBenchPair(cards)) return 950;
        if (this.isAxePair(cards)) return 940;
        if (this.isWindPair(cards)) return 930;
        if (this.isSkyNine(cards)) return 800;
        if (this.isSkyBar(cards)) {
            if (this.getCardValue(cards[0]) == 8) {
                if (this.isRedCard(cards[0])) {
                    return 760;
                } else {
                    return 750;
                }
            } else {
                if (this.isRedCard(cards[1])) {
                    return 760;
                } else {
                    return 750;
                }
            }
        }
        if (this.isEarthBar(cards)) {
            if (this.getCardValue(cards[0]) == 8) {
                if (this.isRedCard(cards[0])) {
                    return 660;
                } else {
                    return 650;
                }
            } else {
                if (this.isRedCard(cards[1])) {
                    return 660;
                } else {
                    return 650;
                }
            }
        }
        var point = this.getMixCardPoint(cards);
        point = point * 10;
        if (this.hasCard(cards, 12)) {
            point = point + 9;
        } else if (this.hasCard(cards, 2)) {
            point = point + 8;
        } else if (this.hasCard(cards, 8, CardDecor.HEART) || this.hasCard(cards, 8, CardDecor.BLOCK)) {
            point = point + 7;
        } else if (this.hasCard(cards, 4, CardDecor.HEART) || this.hasCard(cards, 4, CardDecor.BLOCK)) {
            point = point + 6;
        } else if (this.hasCard(cards, 4, CardDecor.SPADE) || this.hasCard(cards, 4, CardDecor.PLUM)) {
            point = point + 5;
        } else if (this.hasCard(cards, 6, CardDecor.SPADE) || this.hasCard(cards, 6, CardDecor.PLUM)) {
            point = point + 5;
        } else if (this.hasCard(cards, 10, CardDecor.SPADE) || this.hasCard(cards, 10, CardDecor.PLUM)) {
            point = point + 5;
        } else if (this.hasCard(cards, 6, CardDecor.HEART) || this.hasCard(cards, 6, CardDecor.BLOCK)) {
            point = point + 3;
        } else if (this.hasCard(cards, 7, CardDecor.HEART) || this.hasCard(cards, 7, CardDecor.BLOCK)) {
            point = point + 3;
        } else if (this.hasCard(cards, 10, CardDecor.HEART) || this.hasCard(cards, 10, CardDecor.BLOCK)) {
            point = point + 3;
        } else if (this.hasCard(cards, 11)) {
            point = point + 3;
        } else if (this.hasCard(cards, 5)) {
            point = point + 1;
        } else if (this.hasCard(cards, 9)) {
            point = point + 1;
        } else if (this.hasCard(cards, 7, CardDecor.SPADE) || this.hasCard(cards, 7, CardDecor.PLUM)) {
            point = point + 1;
        } else if (this.hasCard(cards, 8, CardDecor.SPADE) || this.hasCard(cards, 8, CardDecor.PLUM)) {
            point = point + 1;
        }
        return point;
    }
}

exports.DXCardMgr = DXCardMgr;